/**
* ...
* @author Default
* @version 0.1
*/

package com.crlclr.ui {
	
	import com.crl.core.IDataRenderer;
	import com.crl.ui.controls.Component;
	import com.crl.utils.FilterUtil;
	import com.crlclr.*;
	import com.crlclr.data.qzone.PictureVO;
	import com.crlclr.events.*;
	import com.crlclr.utils.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.net.*;

	public class QThumb extends Component implements IDataRenderer {
		private var vo:PictureVO;
		private var loader:Loader;
		private var delegate:ThumbLabel;
		
		private var tipString:String;
		private var padding:Padding;
		private var size:QSize;
		
		private var _index:int;
		
		private var defaultColor:Number = 0xFF99CC;
		private var selectedColor:Number = 0x999999;
		private var selectColor:Number = 0xFF9C00;
		
		public function QThumb() {
			this.buttonMode = true;
			this.size = new QSize(80, 80);
			this.padding = new Padding(5,15);
			super();
		}
		
		override protected function create():void {
			this.loader = new Loader();
			
			var rect:Rectangle=this.size.getRect();
			rect.width-=this.padding.padX*2;
			this.loader.scrollRect=rect;
			this.delegate = new ThumbLabel();
			
			addChild(this.loader);
			addChild(this.delegate);
		}
		protected override function bind():void {
			this.addEventListener(MouseEvent.CLICK, mouseHander);
		}
		protected function mouseHander(event:MouseEvent):void {
			var bookEvent:BookEvent = new BookEvent(BookEvent.THUMBCHANGET);
			bookEvent.index = this._index;
			this.dispatchEvent(bookEvent);
		}
		
		protected override function draw() : void{
			
			var g:Graphics = this.graphics;
			g.beginFill(0xffffff,.5);
			g.drawRect(0, 0, size.width, size.height);
			
			delegate.x = delegate.y = padding.padX;
		}
		
		public function set index(value:int):void{
			this._index=value;
		}
		
		public function get index():int{
			return this._index;
		}
		
		public function set data(value:*):void {
			this.vo = value as PictureVO;
			var path:String = this.vo.thumb;
			tipString = this.vo.desc;
			
			if(tipString==""){
				delegate.text=(this.index+1)+"";
			}else{
				delegate.text=tipString;
			}
			load(path);
		}
		public function get data():* {
			return this.vo;
		}
		
		
		private function load(path:String):void {
			var urlrequest:URLRequest = new URLRequest(path);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, initHander);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			loader.load(urlrequest);
		}
		
		private function initHander(event:Event):void {
			event.target.removeEventListener(Event.INIT, initHander);
			loader.x = loader.y = this.padding.padX;
		}
		
		private function errorHandler(event:Event):void{
			trace("crl:"+event);
		}
		
		
		
		public function select():void {
			if(this.hasEventListener(MouseEvent.CLICK)){
				this.removeEventListener(MouseEvent.CLICK, mouseHander);
			}
			var g:Graphics =this.graphics;
			g.beginFill(selectColor);
			g.drawRect(0, 0, size.width, size.height);
			
			this.delegate.y = -padding.padY;
			this.delegate.x = 0;
			this.delegate.select();
		}
		
		
		private var _drak:Array;
		private function get drak():Array {
			if(_drak){
				_drak=[FilterUtil.disableFilter()];
			}
			return _drak;
		}
		
		public function unselect():void {
			if(!this.hasEventListener(MouseEvent.CLICK)){
				this.addEventListener(MouseEvent.CLICK, mouseHander);
			}
			var g:Graphics =this.graphics;
			g.beginFill(selectedColor,.5);
			g.drawRect(0, 0, size.width, size.height);
			this.loader.filters = this.drak
			this.delegate.y = this.delegate.x = this.padding.padX;
			this.delegate.unselect();
		}
	}
	
}
